//************************************************************
//	COPYRIGHT 2011 Birthin' Hips - ALL RIGHTS RESERVED
//************************************************************
//
//	$Id: MessageQueue.java$
//
//	$Date: 12/13/11$
//
//	Reason: Passive (static) queue object, with two buffers for passing messages between threads. All functionality
//			can be accessed statically by any other member of the program.
//
//*************************************************************

/** $Author: Colton Presler, Ross Kahn, Elysia Haight$ */
/** $Revision: $ */


package com;

import java.util.concurrent.LinkedBlockingQueue;

//==============================================================
public class MessageQueue {
	
	public static final int QUEUE_CAPACITY = 1000;	// Maximum number of elements a queue can have. Set lower to observe thread interference more easily
	
	// Read and Write queues are linked blocking queues, which provides the functionality that any requests to pull data from them
	// will be put on hold until at least one data element exists in the queue. 
	private static LinkedBlockingQueue<Message> readQueue = new LinkedBlockingQueue<Message>(QUEUE_CAPACITY);
	private static LinkedBlockingQueue<Message> writeQueue = new LinkedBlockingQueue<Message>(QUEUE_CAPACITY);
	
	/**
	 * Constructor. Because all functionality is static in this class, constructor has no functionality
	 */
	//----------------------------------------------------------
	private MessageQueue(){
		// Default, no functionality
	}
	
	/**
	 * Insert to the read queue
	 * 
	 * @param msg	The message to be inserted
	 */
	//----------------------------------------------------------
	public static void insertToReadQueue(Message msg){
			readQueue.add(msg);
	}
	
	/**
	 * Remove from the read queue. If no elements are present, wait until they are.
	 * 
	 * @return 	The message to be removed from read queue and reflected to write queue
	 */
	//----------------------------------------------------------
	public static Message pullFromReadQueue(){
		try {
			return readQueue.take();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * Insert to the write queue
	 * 
	 * @param msg	The message to be inserted
	 */
	//----------------------------------------------------------
	public static void insertToWriteQueue(Message msg){
		writeQueue.add(msg);
	}
	
	/**
	 * Remove from the write queue. If no elements are present, wait until they are.
	 * 
	 * @return	The message to be removed from write queue, to be verified and logged
	 */
	//----------------------------------------------------------
	public static Message pullFromWriteQueue(){
		try {
			return writeQueue.take();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return null;
	}
}
